package com.srs.dao.info;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.agent.UseWeakRx;
import com.srs.util.HibernateUtil;

public class SampleRxQualityDao {

	final static Logger logger = LoggerFactory.getLogger(SampleRxQualityDao.class);

	private List<UseWeakRx> userlist;
	private UseWeakRx user;

	public void delete(Integer id) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			user = (UseWeakRx) session.get(UseWeakRx.class, id);
			session.delete(user);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<UseWeakRx> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			userlist = session.createQuery("from User").list();
			return userlist;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	public UseWeakRx get(Integer id) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.uid=:id");
			q.setInteger("userid", id);
			return (UseWeakRx) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(UseWeakRx usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(UseWeakRx usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
